package org.example.subside.List;


import org.example.subside.List.pojo.Person;

import java.util.*;

/**
 * @Author : zhangbo
 * @data : 2024/5/16
 * @description :聚合（max/min/count)
 */
public class StreamTest3 {
    public static void main(String[] args) {
        //1.获取String集合中最长的元素
        List<String> list = Arrays.asList("adnm", "admmt", "pot", "xbangd", "weoujgsd");
        Optional<String> max = list.stream().max(Comparator.comparing(String::length));
        System.out.println("最长的字符串：" + max.get());

        //2.获取Integer集合中的最大值
        List<Integer> list1 = Arrays.asList(7, 6, 9, 4, 11, 6);
        // 自然排序
        Optional<Integer> max1 = list1.stream().max(Integer::compareTo);
        // 自定义排序（从大到小排序）
        Optional<Integer> max2 = list1.stream().max((o1, o2) -> o2 - o1);
        System.out.println("自然排序的最大值：" + max1.get());
        System.out.println("自定义排序的最大值：" + max2.get());
        //3.获取员工薪资最高的人
        List<Person> personList = new ArrayList<Person>();
        personList.add(new Person("Tom", 8900, 23, "male", "NewYork"));
        personList.add(new Person("Jack", 7000, 25, "male", "Washington"));
        personList.add(new Person("Lily", 7800, 21, "female", "Washington"));
        personList.add(new Person("Anni", 8200, 24, "female", "NewYork"));
        personList.add(new Person("Owen", 9500, 25, "male", "NewYork"));
        personList.add(new Person("Alisa", 7900, 26, "female", "NewYork"));
        Optional<Person> optional = personList.stream().max(Comparator.comparing(Person::getSalary));
        Optional<Person> optional2 = personList.stream().min(Comparator.comparing(Person::getSalary));
        System.out.println("薪资最高："+optional);
        System.out.println("薪资最低："+optional2);

        //4.计算Integer集合中大于6的元素的个数
        List<Integer> list4 = Arrays.asList(7, 6, 4, 8, 2, 11, 9);
        long count = list4.stream().filter(x -> x > 6).count();
        System.out.println("list中大于6的元素个数：" + count);
    }
}
